package epucjr.psicopuc.teste.testeDemoTableButton;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.table.AbstractTableModel;

public class EmployeeTableModel extends AbstractTableModel {

    /*private static final Employee[] employees = new Employee[]{
        new Employee("Quintin", "Beukes"),
        new Employee("Anita", "Geber"),
        new Employee("Gavin", "Classen"),
        new Employee("Arie", "Kruger")
    };
     * 
     */

    List<Employee> employees = new ArrayList<Employee>();


    private Map<Employee, JButton> buttons = new HashMap<Employee, JButton>();
    private EmployeeButtonHandler handler;

    public EmployeeTableModel(EmployeeButtonHandler handler) {
        this.handler = handler;
        employees.add(new Employee("Quintin", "Beukes"));
        employees.add(new Employee("Anita", "Geber"));
        employees.add(new Employee("Gavin", "Classen"));
        employees.add(new Employee("Arie", "Kruger"));
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Name";
            case 1:
                return "Surnam";
            case 2:
                return "Options";
            case 3:
                return "Options";
        }
        return "#invalid";
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if (columnIndex == 2 || columnIndex == 3) {
            return JButton.class;
        } else {
            return String.class;
        }
    }

    public int getRowCount() {
        //return employees.length;
        return employees.size();
    }

    public int getColumnCount() {
        return 4;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0:
               // return employees[rowIndex].getName();
                return employees.get(rowIndex).getName();
            case 1:
              //  return employees[rowIndex].getSurname();
                return employees.get(rowIndex).getSurname();
            case 2:
              //  return getCellButton(employees[rowIndex]);
                return getCellButton(employees.get(rowIndex));
            case 3:
              //  return getCellButton(employees[rowIndex]);
                return getCellButton(employees.get(rowIndex));
        }

        return "#invalid" + rowIndex + " AND " + columnIndex;
    }

    private JButton getCellButton(Employee employee) {
        JButton button = buttons.get(employee);
        if (button == null) {
            button = createButton(employee);
            buttons.put(employee, button);
        }
        return button;
    }

   /* public void insertEmptyRow() {
        String[] rowData = new String[columnNames.length];
        for (int i = 0; i < columnNames.length; i++) {
            rowData[i] = "";
        }
        rows_data.addElement(rowData);
        fireTableChanged(null);
    }
    * 
    */

    public void insertRow(Employee employee){
        this.employees.add(employee);
        fireTableRowsInserted(0, this.employees.size());
    }

    private JButton createButton(final Employee employee) {
        JButton button = null;
        System.out.println("Nome = " + employee.getName());
        if (employee.getName().equals("Anita")) {
            button = new JButton();
            button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/epucjr/psicopuc/images/BuscaResultado1.jpg")));
        } else {
            button = new JButton("Get Fullname");
        }


        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                handler.buttonClicked(employee);
                System.out.println("e = " + "Teste");
                insertRow(new Employee("Carduns", "Biba"));
            }
        });

        return button;
    }

    public static interface EmployeeButtonHandler {

        public void buttonClicked(Employee e);
    }
}
